package com.li.shanlin.controller;

import java.io.File;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.li.shanlin.baseUtils.FacePlusUtils;
import com.li.shanlin.utils.BaseFileUtil;

/**
 * face++人脸识别
 * 
 * @author liyinlong
 *
 *         2017年3月13日上午11:51:02
 */
@Controller
@RequestMapping("/api/face")
public class FaceController {
	/**
	 * 照片，与身份证比对，取信值最大100
	 * 
	 * @param image
	 * @param shenfen
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping("/picvscard")
	@ResponseBody
	public String getPicvscard(@RequestParam("image") String image, @RequestParam("shenfen") String shenfen,
			HttpServletRequest request) {

		String filePath1 = request.getSession().getServletContext().getRealPath("/") + "static/upload/";
		File filePath1_f = new File(filePath1);

		if (filePath1_f.isDirectory()) {
			filePath1_f.mkdirs();
		}
		try {
			BaseFileUtil.decoderBase64File(image, filePath1 + "image.jpg");
			BaseFileUtil.decoderBase64File(shenfen, filePath1 + "shenfen.jpg");
		} catch (Exception e) {
			e.printStackTrace();

		}
		File image1 = new File(filePath1 + "image.jpg");
		File shenfen1 = new File(filePath1 + "shenfen.jpg");
		String result1 = FacePlusUtils.faceDetectMess(image1);
		String result2 = FacePlusUtils.faceOcridcard(shenfen1);
		// 打印face_token
		JSONObject json1 = JSON.parseObject(result1);
		List<Map<String, Object>> json2 = (List<Map<String, Object>>) json1.get("faces");
		Map<String, Object> faces = json2.get(0);
		String face_token = (String) faces.get("token");
		// 打印身份信息
		Map<String, Object> json3 = JSON.parseObject(result2);
		String idcard_name = (String) json3.get("name");
		String idcard_number = (String) json3.get("id_card_number");
		String activeAndFacetoken = FacePlusUtils.faceVerifyActiveAndFacetoken(idcard_name, idcard_number, face_token);
		System.out.println("activeAndFacetoken:" + activeAndFacetoken);
		if (image1.exists()) {
			image1.delete();
		}
		if (shenfen1.exists()) {
			shenfen1.delete();
		}
		return activeAndFacetoken;
	}

	/**
	 * 身份证信息扫描
	 * 
	 * @param identityCard
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping("/card")
	@ResponseBody
	public String getIdentityCardInfo(HttpServletRequest request) {
		String identityCard = request.getParameter("identityCard");
		identityCard = identityCard.replaceAll(" ", "");
		System.out.println("identityCard:"+identityCard);
		String filePath2 = request.getSession().getServletContext().getRealPath("/") + "static/upload/shenfen.jpg";
		try {
			BaseFileUtil.decoderBase64File(identityCard, filePath2);
			// 转存图片
			// BaseFileUtil.toFile(identityCard, filePath2+"shenfen.jpg");
		} catch (Exception e) {
			e.printStackTrace();
		}
		File shenfen = new File(filePath2);
		String result2 = FacePlusUtils.faceOcridcard(shenfen);
		System.out.println("result2:" + result2);
		return result2;

	}

	/**
	 * 从图片中得到base64
	 * 
	 * @param image
	 * @param request
	 * @return
	 */
	@RequestMapping("/getBase")
	public String getBase(@RequestParam("image") MultipartFile image, HttpServletRequest request) {
		String filePath1 = null;
		if (!image.isEmpty()) {
			try {
				// 文件保存路径
				filePath1 = request.getSession().getServletContext().getRealPath("/") + "static/upload/"
						+ image.getOriginalFilename();
				// 转存文件
				image.transferTo(new File(filePath1));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		String image1 = null;
		try {
			image1 = BaseFileUtil.encodeBase64File(filePath1);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		File file1 = new File(filePath1);
		if (file1.exists()) {
			file1.delete();
		}

		request.getSession().setAttribute("identityCard", image1);
		return "redirect:/api/face/card";
	}

	/**
	 * 从图片中得到base64
	 * 
	 * @param image
	 * @param request
	 * @return
	 */
	@RequestMapping("/test")
	@ResponseBody
	public String getBaTest(@RequestParam("image") MultipartFile image, HttpServletRequest request) {

		String filePath1 = null;
		if (!image.isEmpty()) {
			try {
				// 文件保存路径
				filePath1 = request.getSession().getServletContext().getRealPath("/") + "static/upload/"
						+ image.getOriginalFilename();
				// 转存文件
				image.transferTo(new File(filePath1));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		String image1 = null;
		try {
			image1 = BaseFileUtil.encodeBase64File(filePath1);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		File file1 = new File(filePath1);
		if (file1.exists()) {
			file1.delete();
		}

		request.getSession().setAttribute("image", image1);
		return image1;
	}
	@RequestMapping("/card1")
	@ResponseBody
	public String getIdentityCardInfo1(@RequestParam("image") MultipartFile image,HttpServletRequest request) {
		String filePath1 = null;
		if (!image.isEmpty()) {
			try {
				// 文件保存路径
				filePath1 = request.getSession().getServletContext().getRealPath("/") + "static/upload/"
						+ image.getOriginalFilename();
				// 转存文件
				image.transferTo(new File(filePath1));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		File image1 = new File(filePath1);
		String result2 = FacePlusUtils.faceOcridcard(image1);
		if (image1.exists()) {
			image1.delete();
		}
		/*System.out.println("result2:" + result2);
		String s = null;
		try {
			s = URLDecoder.decode(result2,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		return result2;

	}

}
